<?php


namespace App\Http\Admin\Controllers;

use Illuminate\Http\Request;
use App\Http\Admin\Controllers\Controller as Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;




class BaiPiController  extends Controller{

    public function __construct(Request $request){
        $token = getToken($request);
        $uid =Redis::get("code:".$token);
        $this->uid=$uid;
    }

    /**
     * 创建白皮书  
    */
    public function add_baipi(Request $request){

        $data=$request->only('title');
        //获取创建基本数据 默认覆盖修改
        $data=data_add($data,$this->uid);

        $res=DB::connection("mysql2")->table("c_baipi")->insertGetId($data);

        return [
            'status_code'      => 200,
            'message'     =>  '新增成功',
            'data' => $res
        ];

    }

    /**
     * 删除白皮书
     */
    public function delete_baipi($id){
        $data=['status'=>9];

        //获取修改基本数据
        $data=data_update($data,$this->uid);
        $res=DB::connection("mysql2")->table('c_baipi')->where('id',$id)->update($data);
        return [
            'status_code'      => 200,
            'message'     =>  '新增成功',
            'data' => $res
        ];
    }

    /**
     * 修改白皮书
     */

    public function update_baipi(Request $request,$id){
        $data=$request->only('title');

        //获取修改基本数据
        $data=data_update($data,$this->uid);

        $res=DB::connection("mysql2")->table('c_baipi')->where('id',$id)->update($data);
        return [
            'status_code'      => 200,
            'message'     =>  '新增成功',
            'data' => $res
        ];

        
    }

    /**
     * 查询白皮书
     */
    public function select_baipi(){

        $res=DB::connection("mysql2")->table('c_baipi')->where('status',1)->get();

        

        return [
            'status_code'      => 200,
            'message'     =>  '新增成功',
            'data' => $res
        ];
        
    }


    /**
     *  新增正则
     */
    public function add_regex(Request $request){

        $data=$request->only('title','content','type');

        //获取创建基本数据 默认覆盖修改
        $data=data_add($data,$this->uid);

        $res=DB::connection("mysql2")->table("c_regex")->insertGetId($data);

        return [
            'status_code'      => 200,
            'message'     =>  '新增成功',
            'data' => $res
        ];

    }
    /**
     *  修改正则
     */
    public function update_regex(Request $request,$id){
        $data=$request->only('title','content','type');

        $data=data_update($data,$this->uid);

        $res=DB::connection("mysql2")->table("c_regex")->where('id',$id)->update($data);

        return [
            'status_code'      => 200,
            'message'     =>  '新增成功',
            'data' => $res
        ];
    }

    /**
     *  查询正则
     */
    public function select_regex(Request $request){
        $data=$request->only('cbId');

        if(array_key_exists('cbId',$data)){
            $bid=DB::connection("mysql2")->table('c_b_r')->where([
                ['cbId',$data['cbId']],
                ['status',1]
            ])->select("crId")->get()->toArray();
            $crIds=array_column($bid,"crId");
            $res=DB::connection("mysql2")->table('c_regex')->whereNotIn("id",$crIds)->where('status',1)->get()->toArray();


        }else{
            $res=DB::connection("mysql2")->table('c_regex')->where('status',1)->get();
        }


        return [
            'status_code'      => 200,
            'message'     =>  '新增成功',
            'data' => $res
        ];
    }
    /**
     *  删除正则
     */
    public function delete_regex($id){
        $data=['status'=>9];

        //获取修改基本数据
        $data=data_update($data,$this->uid);
        $res=DB::connection("mysql2")->table('c_regex')->where('status',1)->update($data);
        return [
            'status_code'      => 200,
            'message'     =>  '删除成功',
            'data' => $res
        ];
    }


    /**
     * 正则绑定白皮书
     */

     public function add_baipi_regex(Request $request){
        $data=$request->only('cbId','crId');
         //获取创建基本数据 默认覆盖修改
         $data=data_add($data,$this->uid);

        $data['type']=DB::connection("mysql2")->table("c_regex")->where("id",$data['crId'])->first()['type'];

         $res=DB::connection("mysql2")->table("c_b_r")->insertGetId($data);
         DB::connection("mysql2")->table("c_b_r")->where("id",$res)->update(['orderId'=>$res]);
         return [
             'status_code'      => 200,
             'message'     =>  '新增成功',
             'data' => $res
         ];
     }


     /**
     * 删除正则绑定白皮书
     */

    public function delete_baipi_regex($id){
        $data=['status'=>9];

        //获取修改基本数据
        $data=data_update($data,$this->uid);

        $res=DB::connection("mysql2")->table('c_b_r')->where([
            ['status',1],
            ['id',$id]
        ])->update($data);
        return [
            'status_code'      => 200,
            'message'     =>  '删除成功',   
            'data' => $res
        ];
    }


    /**
     * 上下移动白皮书列表
     */

    public function bt_baipi_regex(Request $request){
        $data=$request->only('cbrId','type','cbId');
        $olddata=DB::connection("mysql2")->table('c_b_r')->where('id',$data['cbrId'])->first();

        if($data['type']==1){
            $newdata=DB::connection("mysql2")->table('c_b_r')->where([
                ['cbId',$data['cbId']],
                ['orderId',"<",$olddata['orderId']]
            ])->orderBy("orderId","desc")->first();
        }else{
            $newdata=DB::connection("mysql2")->table('c_b_r')->where([
                ['cbId',$data['cbId']],
                ['orderId',">",$olddata['orderId']]
            ])->first();
        }

        if($newdata){
            $res=DB::connection("mysql2")->table('c_b_r')->where('id',$olddata['id'])->update([
                'orderId'=>$newdata['orderId']
            ]);
            $res=DB::connection("mysql2")->table('c_b_r')->where('id',$newdata['id'])->update([
                'orderId'=>$olddata['orderId']
            ]);
        }else{
            $res="无修改内容";
        }

        return [
            'status_code'      => 200,
            'message'     =>  '修改成功',   
            'data' => $res
        ];
    }

    /**
     * 查询正则绑定白皮书
     */

    public function select_baipi_regex(Request $request,$bId){
        $data=$request->only('type');

        if($data['type']==1){
            $res=DB::connection("mysql2")->table('c_b_r as cbr')
            ->leftJoin("c_regex as cr","cr.id","=","cbr.crId")
            ->where([
            ['cbr.status',1],
            ['cbr.type',"!=",2],
            ['cbr.cbId',$bId]
            ])->select("cbr.*","cr.title")->orderBy('orderId')->get();
        }else{
            $res=DB::connection("mysql2")->table('c_b_r as cbr')
            ->leftJoin("c_regex as cr","cr.id","=","cbr.crId")
            ->where([
            ['cbr.status',1],
            ['cbr.type',$data['type']],
            ['cbr.cbId',$bId]
            ])->select("cbr.*","cr.title")->orderBy('orderId')->get();
        }

        

        return [
            'status_code'      => 200,
            'message'     =>  '查询成功',
            'data' => $res
        ];

    }



    /**
     * 查询正则绑定白皮书
     */

    public function select_baipi_regex_answer(Request $request,$bId){
        $data=$request->only('type','cid');

        $res=DB::connection("mysql2")->table('c_b_r as cbr')
            ->leftJoin("c_regex as cr","cr.id","=","cbr.crId")
            ->where([
            ['cbr.status',1],
            ['cbr.type',$data['type']],
            ['cbr.cbId',$bId]
            ])->select("cbr.*","cr.title")->orderBy('orderId')->get()->toArray();

        if($data['type']==1){
            $this->add_case_baipi_answer($data['cid'],$res);
            $this->add_case_baipi_answer_add($data['cid'],$bId);
            $res=DB::connection("mysql2")->table('c_b_r as cbr')
            ->leftJoin("c_regex as cr","cr.id","=","cbr.crId")
            ->leftJoin("c_answer as ca","ca.cbrId","=","cbr.id")
            ->where([
            ['cbr.status',1],
            ['cbr.type',"!=",2],
            ['cbr.cbId',$bId],
            ['ca.cid',$data['cid']],
            ])->select("cbr.*","cr.title",'ca.answer')->orderBy('orderId')->get();

        }else{
            $res=DB::connection("mysql2")->table('c_b_r as cbr')
            ->leftJoin("c_regex as cr","cr.id","=","cbr.crId")
            ->leftJoin("c_answer as ca","ca.cbrId","=","cbr.id")
            ->where([
            ['cbr.status',1],
            ['cbr.type',$data['type']],
            ['cbr.cbId',$bId],
            ['ca.cid',$data['cid']],
            ])->select("cbr.*","cr.title",'ca.answer')->orderBy('orderId')->get();
        }


        return [
            'status_code'      => 200,
            'message'     =>  '查询成功',
            'data' => $res
        ];

    }



    public function add_case_baipi_answer($cid,$data){
        $cbrIds=array_column($data,'id');
        $cbrData=DB::connection("mysql2")->table("c_answer")->where('cid',$cid)->whereIn('cbrId',$cbrIds)->get()->toArray();
        $cbrDataIds=array_column($cbrData,'cbrId');
        foreach($data as $k=>$v){
            if(in_array($v['id'],$cbrDataIds)) unset($data[$k]);
        }
        $caseData=DB::table("c_case_data")->where("cid",$cid)->first();
        foreach($data as $k=>$v){
            $crData=DB::connection('mysql2')->table("c_regex")->where("id",$v['crId'])->first();
            if($crData['type']==1){
                $reg=explode("&",$crData['content']);
                preg_match($reg[0],$caseData['content'],$res);
                
                if(count($res)>=$reg[1]+1){
                    $ret=$res[$reg[1]];
                    DB::connection('mysql2')->table("c_answer")->insertGetId([
                        'answer'=>$ret,
                        'cid'=>$cid,
                        'cbrId'=>$v['id']
                    ]);
                }

               

                
            
            }else if($crData['type']==3){
                $ret=123;
            }
        }
    }


    public function add_case_baipi_answer_add($cid,$baipiId){
        $caseData=DB::table("c_case")->where("id",$cid)->first();
        $caseContent=DB::table("c_case_data")->where("cid",$cid)->first();
        $anR15=DB::connection("mysql2")->table("c_regex as cr")
        ->leftJoin("c_b_r as cbr","cr.id",'=','cbr.crId')
        ->where([
            ['cbr.cbId',$baipiId],
            ['cbr.status',1],
            ['cr.status',1],
            ['cr.id',87]
        ])->select('cbr.*')->first();
        $anR15Ans=DB::connection("mysql2")->table("c_answer")->where([
            ['cbrId',$anR15['id']],
            ['cid',$cid]
        ])->first();
        if($anR15&&!$anR15Ans){
              //诉讼级别
                $ans15=$caseData['subject'];
                if($ans15==1){
                    $ans15="一审";
                }else if($ans15==2){
                    $ans15="二审";
                }
                DB::connection("mysql2")->table("c_answer")->insertGetId([
                    'cid'=>$cid,
                    'answer'=>$ans15,
                    'cbrId'=>$anR15['id']
                ]);
        }

        $anR16=DB::connection("mysql2")->table("c_regex as cr")
        ->leftJoin("c_b_r as cbr","cr.id",'=','cbr.crId')
        ->where([
            ['cbr.cbId',$baipiId],
            ['cbr.status',1],
            ['cr.status',1],
            ['cr.id',88]
        ])->select('cbr.*')->first();
         $anR16Ans=DB::connection("mysql2")->table("c_answer")->where([
            ['cbrId',$anR16['id']],
            ['cid',$cid]
        ])->first();
        if($anR16&&!$anR16Ans){
            //家理案号
            $ans16=$caseData['master_number'];
            DB::connection("mysql2")->table("c_answer")->insertGetId([
                'cid'=>$cid,
                'answer'=>$ans16,
                'cbrId'=>$anR16['id']
            ]);
        }
        

        $anR17=DB::connection("mysql2")->table("c_regex as cr")
        ->leftJoin("c_b_r as cbr","cr.id",'=','cbr.crId')
        ->where([
            ['cbr.cbId',$baipiId],
            ['cbr.status',1],
            ['cr.status',1],
            ['cr.id',89]
        ])->select('cbr.*')->first();
        $anR17Ans=DB::connection("mysql2")->table("c_answer")->where([
            ['cbrId',$anR17['id']],
            ['cid',$cid]
        ])->first();
        if($anR17&&!$anR17Ans){
             //判决时间
            $ans17=$caseData['case_time'];
            DB::connection("mysql2")->table("c_answer")->insertGetId([
                'cid'=>$cid,
                'answer'=>$ans17,
                'cbrId'=>$anR17['id']
            ]);
        }
       
        $anR18=DB::connection("mysql2")->table("c_regex as cr")
        ->leftJoin("c_b_r as cbr","cr.id",'=','cbr.crId')
        ->where([
            ['cbr.cbId',$baipiId],
            ['cbr.status',1],
            ['cr.status',1],
            ['cr.id',90]
        ])->select('cbr.*')->first();
        $anR18Ans=DB::connection("mysql2")->table("c_answer")->where([
            ['cbrId',$anR18['id']],
            ['cid',$cid]
        ])->first();
        if($anR18&&!$anR18Ans){
            //案件地区
            $ans18=DB::table("city")->where("id",$caseData['city'])->first();
            if($ans18){
                $ans18=$ans18['name'];
            }else{
                $ans18="";
            }
            DB::connection("mysql2")->table("c_answer")->insertGetId([
                'cid'=>$cid,
                'answer'=>$ans18,
                'cbrId'=>$anR18['id']
            ]);
        }
        
        $anR19=DB::connection("mysql2")->table("c_regex as cr")
        ->leftJoin("c_b_r as cbr","cr.id",'=','cbr.crId')
        ->where([
            ['cbr.cbId',$baipiId],
            ['cbr.status',1],
            ['cr.status',1],
            ['cr.id',91]
        ])->select('cbr.*')->first();
        $anR19Ans=DB::connection("mysql2")->table("c_answer")->where([
            ['cbrId',$anR19['id']],
            ['cid',$cid]
        ])->first();
        if($anR19&&!$anR19Ans){
            //案由
            $ans19=DB::table("c_case_action")->where("id",$caseData['case_action'])->first();

            if($ans19){
                $ans19=$ans19['name'];
            }else{
                $ans19="";
            }
            DB::connection("mysql2")->table("c_answer")->insertGetId([
                'cid'=>$cid,
                'answer'=>$ans19,
                'cbrId'=>$anR19['id']
            ]);
        }


        $anR20=DB::connection("mysql2")->table("c_regex as cr")
        ->leftJoin("c_b_r as cbr","cr.id",'=','cbr.crId')
        ->where([
            ['cbr.cbId',$baipiId],
            ['cbr.status',1],
            ['cr.status',1],
            ['cr.id',161]
        ])->select('cbr.*')->first();
        $anR20Ans=DB::connection("mysql2")->table("c_answer")->where([
            ['cbrId',$anR20['id']],
            ['cid',$cid]
        ])->first();
        if($anR20&&!$anR20Ans){
            // $caseContent;
            // dd($caseContent['content']);
            $zesre="/(案件受理费|诉讼费)(.*?)元/u";
            preg_match($zesre,$caseContent['content'],$ans);
            if(count($ans)>1){
                DB::connection("mysql2")->table("c_answer")->insertGetId([
                    'cid'=>$cid,
                    'answer'=>$ans[2],
                    'cbrId'=>$anR20['id']
                ]);
            }
        }


        $cbrData=DB::connection("mysql2")->table("c_answer")->where('cid',$cid)->select('cbrId')->get()->toArray();
        $crIds=array_column($cbrData,"cbrId");
        $baipiData=DB::connection("mysql2")->table("c_b_r")->where([
            ["cbId",$baipiId],
            ['type',4]
        ])->whereNotIn('id',$crIds)->get()->toArray();

        foreach($baipiData as $k=>$v){
            DB::connection("mysql2")->table("c_answer")->insertGetId([
                'answer'=>"",
                'cid'=>$cid,
                'cbrId'=>$v['id']
            ]);
        }


        


    }







    

     








}